package com.iwaster.servlet;

import com.iwaster.utils.UploadUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/*
 *${PACKAGE_NAME}
 *@author Ouka
 *10:12
 */
@WebServlet(name = "ListServlet",urlPatterns = "/listfile")
public class ListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HashMap<String,String> filenames = new HashMap<>();
        String path = this.getServletContext().getRealPath("/WEB-INF/upload");
        UploadUtils.listFile(new File(path),filenames);
        Set<String> strings = filenames.keySet();
        Iterator<String> iterator = strings.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

        request.setAttribute("filename", filenames);

        System.out.println("-----------------------------");
        request.getRequestDispatcher("/listfile.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
